package com.mla.fastdfs.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.dto.PermissionDto;
import com.mla.fastdfs.pojo.Permission;
import com.mla.fastdfs.pojo.User;
import com.mla.fastdfs.service.PermissionService;
import com.mla.fastdfs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * z
 *
 * @author Jun
 * @version v1.0
 * @date 2020-08-17
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionservice;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Permission record) {
        if (StringUtil.isEmpty(record.getName())) {
            return Result.error("权限名称不能为空");
        }
        if (StringUtil.isEmpty(record.getCode())) {
            return Result.error("权限编号不能为空");
        }
        if (StringUtil.isEmpty(record.getPage())) {
            return Result.error("权限路径不能为空");
        }
        if (record.getIstype() == null) {
            return Result.error("权限分类不能为空");
        }
        return permissionservice.savePerm(record);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(Integer id) {
        if (id == null) {
            return Result.error("id不能为空");
        }

        permissionservice.del(id);
        return Result.ok();
    }

    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public Result selectByPrimaryKey(Integer id) {
        if (id == null) {
            return Result.error("id不能为空");
        }

        Permission info = permissionservice.selectByPrimaryKey(id);
        return Result.ok(info);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getList(Permission record) {
        /**条件判断**/


        List<Permission> list = permissionservice.getList(record);
        return Result.ok(list);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public Result pageList(Permission record,
                                 @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        /**条件判断**/


        PageInfo<Permission> info = permissionservice.pageList(record, pageNo, pageSize);
        return Result.ok(info);
    }

    @RequestMapping(value = "/treeList", method = RequestMethod.POST)
    public Result treeList(Permission record) {
        List<PermissionDto> treeList = permissionservice.treeList(record);
        return Result.ok(treeList);
    }

    @RequestMapping(value = "/getMenu", method = RequestMethod.POST)
    public Result getMenu(String aToken) {
        User user = userService.getUser(aToken);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return permissionservice.getMenu(user.getId());
    }
}